﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.IO;
namespace UnitTest
{


    /// <summary>
    ///This is a test class for CommonUtilTest and is intended
    ///to contain all CommonUtilTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CommonUtilTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


    
        /// <summary>
        /// Test Sql CE Funtion
        /// </summary>
        [TestMethod()]
        public void TestSqlceConstruction()
        {
            bool expected = true;
            bool actual = false;
            
            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
            core.Open(CoreEA.ConnSTR.DbConnectionString.SSCE.GetSSCEConnectionString("D:\\SimpleSchemaCE35.sdf", "", false));
            DataTable ds = core.GetColumnInfoFromTable("df");

            string targetFile = "D:\\ds.xml";

            ds.WriteXml(targetFile);
            if (File.Exists(targetFile))
            {
                actual = true;
            }

            Assert.AreEqual(expected, actual);

        }


        /// <summary>
        /// Test 
        /// </summary>
        [TestMethod()]
        public void ImportDataFromExcelToSqlserver()
        {
            bool expected = true;
            bool actual = false;
            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.OleDb).X_Handler;
            string srcFile = "C:\\1.xls";
            try
            {
                core.Open(CoreEA.ConnSTR.DbConnectionString.Excel.GetOleDbConnectionString(srcFile, true));
                if (core.IsOpened)
                {
                    foreach (string item in core.GetTableListInDatabase(srcFile))
                    {
                        DataTable ds = core.GetAllDataFromTable("[" + item + "]");
                        ds.WriteXml("C:\\" + item.Substring(0, 2) + ".xml");

                    }
                }

                actual = true;
            }
            catch
            {

            }
            finally
            {
                if (core.IsOpened)
                {
                    core.Close();
                }
            }
            Assert.AreEqual(expected, actual);
        }
    }
}
